package com.zzptc.liaotian_android;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zzptc.liaotian_android.bean.ShopInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MainActivity extends Activity {
    String etInput_i;
    ShopInfo shopInfo;
    @BindView(R.id.tv_dialog)
    TextView tvDialog;
    @BindView(R.id.et_input)
    EditText etInput;
    @BindView(R.id.btn_send)
    Button btnSend;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;
    @BindView(R.id.tv_dialog_1)
    TextView tvDialog1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

    }

    @OnClick(R.id.btn_send)
    public void onViewClicked() {
        etInput_i = etInput.getText().toString();
        new AsyncTask<String, Void, String>() {
            @Override
            protected String doInBackground(String... strings) {
                String str = null;
                try {

                    //莉莉
//                    URL url = new URL("http://i.itpk.cn/api.php?question=" + etInput_i);
                    //青云客
                    URL url = new URL("http://api.qingyunke.com/api.php?key=free&appid=0&msg=" + etInput_i);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");

                    if (conn.getResponseCode() == 200) {
                        InputStream is = conn.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                        str = reader.readLine();
                        reader.close();
                        is.close();
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return str;
            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    String result = jsonObject.optString("result");
                    String content = jsonObject.optString("content");

                    shopInfo = new ShopInfo(result, content);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
//                tvDialog.setText("自己 :"+etInput_i);
                tvDialog1.setText(etInput_i + ": 自己");
                tvDialog.setText("菲菲: " + shopInfo.getContent());


//                tvDialog.setText(s);
                etInput.setText("");
            }

            @Override
            protected void onProgressUpdate(Void... values) {
                super.onProgressUpdate(values);
            }

            @Override
            protected void onCancelled(String s) {
                super.onCancelled(s);
            }
        }.execute();

    }


}
